package com.chandra.pos.customer.dao.entity;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

/**
 * Created by Admin on 8/15/14.
 */
public interface CustomerRepository extends JpaRepository<CustomerEntity, Long>, CustomerRepositoryCustom {

    CustomerEntity findByFirstNameAndLastName(String firstName, String name);

    CustomerEntity findByNickNameIgnoreCase(String nickName);

    Page<CustomerEntity> findByFirstNameOrLastNameOrNickNameContainingIgnoreCase(String name, Pageable pageable);

    @Query("SELECT p FROM CustomerEntity p WHERE lower(p.firstName) LIKE %:searchTerm% or  lower(p.lastName) LIKE %:searchTerm% or  lower(p.nickName) LIKE %:searchTerm%")
    public Page<CustomerEntity> search(@Param("searchTerm") String searchTerm, Pageable pageable);

    @Query("SELECT p FROM CustomerEntity p WHERE p.customerType = :customerType")
    public Page<CustomerEntity> search( @Param("customerType") CustomerType customerType, Pageable pageable);


    @Query("SELECT p FROM CustomerEntity p WHERE (lower(p.firstName) LIKE %:searchTerm% or  lower(p.lastName) LIKE %:searchTerm% or  lower(p.nickName) LIKE %:searchTerm%) and p.customerType = :customerType")
    public Page<CustomerEntity> search(@Param("searchTerm") String searchTerm, @Param("customerType") CustomerType customerType, Pageable pageable);

}
